<?php


namespace App\Admin\Controllers\ApiDoc;

use App\Constant\Env;
use App\Http\Controllers\Controller;
use App\Model\Repository\ApiVarRepository;
use Illuminate\Http\Request;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ApiVarController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('Index');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Show interface.
     *
     * @param $id
     * @return Content
     */
    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('Detail');
            $content->description('description');

            $content->body(Admin::show(ApiVarRepository::findOrFail($id), function (Show $show) {

                $show->field('var_id', '变量id');
                $show->field('name', '变量名称');
                $show->field('value', '变量值');
                $show->field('env', '所属环境');
                $show->field('remark', '备注');
                $show->created_at();
                $show->updated_at();
            }));
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('Edit');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('创建');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(ApiVarRepository::class, function (Grid $grid) {

            $grid->column('var_id', '变量id');
            $grid->column('name', '变量名称');
            $grid->column('value', '变量值');
            $grid->column('env', '所属环境');
            $grid->column('remark', '备注');

            $grid->column('created_at', '创建时间');
//            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(ApiVarRepository::class, function (Form $form) {

            $form->display('var_id', '变量id');
            $form->text('name', '变量名称');
            $form->text('value', '变量值');
            $form->select('env', '所属环境')->options(Env::getEnv());
            $form->text('remark', '备注')->default('暂无');

            $form->display('created_at', '创建时间');
            $form->display('updated_at', '修改时间');
        });
    }
}